package edu.bu.met683.TravelDroid.data;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.util.Log;

import com.google.android.maps.GeoPoint;

import edu.bu.met683.TravelDroid.Constants;

/**
 * This class provides static methods to perform various data manipulation
 * tasks.
 */
public class DataUtilities {
	
	public enum TourState{STOPPED, STARTED, PAUSED}
	public static final Double METERS_IN_A_MILES = 1609.344;
	private static final String CLASS_TAG = DataUtilities.class.getSimpleName();

	/**
	 * Converts a string of the form longitude,latitude,alt to a GeoPoint
	 * object.
	 * 
	 * @param geoString
	 *            the string to parse
	 * @return a GeoPoint object
	 */
	public static GeoPoint convertStringToGeoPoint(String geoString) {
		GeoPoint point = null;

		try {
			String[] coordStrings = geoString.split(",");

			// Parse the Long and Lat
			Double lon = Double.parseDouble(coordStrings[0]) * 1E6;
			Double lat = Double.parseDouble(coordStrings[1]) * 1E6;

			// Create the new GeoPoint (remember lat then long here)
			point = new GeoPoint(lat.intValue(), lon.intValue());
		} catch (Exception e) {
			Log.e(CLASS_TAG, "Error Creating GeoPoint", e);
		}
		return point;
	}
	
	public static double ConvertMetersToMiles(float meters)
	{
		try
		{
			return (double)(meters/METERS_IN_A_MILES);
		} 
		catch(Exception e)
		{
			Log.e("ERROR", "ERROR IN CODE - ConvertMetersToMiles:"+ e.toString());
			return 0;
		}
	}
	
	/**
	 * Static method used to create a proximity alert for a particular POI
	 * 
	 * @param c
	 * @param poi
	 */
	public static void createProximityAlert(Context c, PointOfInterest poi) {
		// Check for Audio File String
		if (poi.getAudioFileName().equals(
				PointOfInterest.NO_AUDIO_FILE_STRING)) {
			return;
		}
		
		// Set a reference to the location manager
		LocationManager locationManager = (LocationManager) c
				.getSystemService(Context.LOCATION_SERVICE);

		// Create the Data Uri
		Uri intentData = Uri
				.parse("poi://edu.bu.met683.TravelDroid/poi?poiname="
						+ poi.getName());

		// Create the Intent
		Intent viewDetailIntent = new Intent(
				Constants.INTENT_ACTION_VIEW_POI_DETAIL, intentData);
		PendingIntent pendIntent = PendingIntent.getActivity(c,
				Intent.FLAG_ACTIVITY_NEW_TASK, viewDetailIntent,
				PendingIntent.FLAG_ONE_SHOT);

		// Add the proximity alert to the location manager
		locationManager.addProximityAlert(poi.getPoint().getLatitudeE6() / 1E6,
				poi.getPoint().getLongitudeE6() / 1E6, 50, -1, pendIntent);
	}

	/**
	 * Static method used to create a proximity alert for a particular POI
	 * 
	 * @param c
	 * @param poi
	 */
	public static void removeProximityAlert(Context c, PointOfInterest poi) {
		// Set a reference to the location manager
		LocationManager locationManager = (LocationManager) c
				.getSystemService(Context.LOCATION_SERVICE);

		// Create the Data Uri
		Uri intentData = Uri
				.parse("poi://edu.bu.met683.TravelDroid/poi?poiname="
						+ poi.getName());

		// Create the Intent
		Intent viewDetailIntent = new Intent(
				Constants.INTENT_ACTION_VIEW_POI_DETAIL, intentData);
		PendingIntent pendIntent = PendingIntent.getActivity(c,
				Intent.FLAG_ACTIVITY_NEW_TASK, viewDetailIntent,
				PendingIntent.FLAG_ONE_SHOT);

		// Remove the proximity alert from the location manager
		locationManager.removeProximityAlert(pendIntent);
	}
	
	/**
	 * 
	 *
	 * @param c
	 * @param t
	 */
	public static void createProximityAlertsForTour(Context c, Tour t){
		for( PointOfInterest poi : t.getPoiList() ){
			createProximityAlert(c, poi);
		}
	}
	
	/**
	 * 
	 *
	 * @param c
	 * @param t
	 */
	public static void removeProximityAlertsForTour(Context c, Tour t){
		for( PointOfInterest poi : t.getPoiList() ){
			removeProximityAlert(c, poi);
		}
	}

}
